<template>
  <div style="height:100%;width:100%;position:relative;" ref="maindev">
      <div class="pagetop" :style="{ height:'103px' }">
          <table style="width: 100%; font-size: 12px;" border="0">
              <col style="width: 5%;" />
              <col style="width: 23%;" />
              <col style="width: 5%;" />
              <col style="width: 18%;" />
              <col style="width: 2%;" />
              <col style="width: 4%;" />
              <col style="width: 4%;" />
              <col style="width: 4%;" />
              <col style="width: 4%;" />
              <col style="width: 31%;" />
              <tr>
                  <td align="right">质控日期</td>
                  <td>
                      <el-date-picker v-model="selobj.startdt" @change="queryqcitem" type="date" placeholder="开始日期" value-format="yyyy-MM-dd" style="width: 130px" :clearable="false"></el-date-picker>至    <el-date-picker v-model="selobj.enddt" @change="queryqcitem" type="date" placeholder="结束日期" value-format="yyyy-MM-dd" style="width: 130px" :clearable="false"></el-date-picker>
                  </td>
                  <td align="right">检验仪器</td>
                  <td>
                      <rm-ddl-instrument @change="instrChange" width="202px" v-model="selobj.instrid" prgno="LIS530" :clearable="false" :bPower="true" defaultflag="1"></rm-ddl-instrument>
                  </td>
                  <td></td>
                  <td>
                      <el-checkbox true-label="1" false-label="0" v-model="selobj.freq">显示次数</el-checkbox>
                  </td>
                  <td>
                      <el-checkbox true-label="1" false-label="0" v-model="selobj.user">显示上报人</el-checkbox>
                  </td>
                  <!-- <td>
                      <el-checkbox true-label="1" false-label="0" v-model="selobj.rptitems">多项目按批号导出</el-checkbox>
                  </td> -->
                  <td>
                      <el-checkbox true-label="1" false-label="0" v-model="selobj.point4">精确度小数点后4位</el-checkbox>
                  </td>
              </tr>
              <tr>
                  <td align="right">数据模式</td>
                  <td colspan="1" style="text-align: left;">
                      <el-radio v-model="selobj.gtype" label="3" @change="Btnquery">平均值</el-radio>
                      <el-radio style="width:67px" v-model="selobj.gtype" label="1" @change="Btnquery">最后点</el-radio>
                      <el-radio v-model="selobj.gtype" label="2" @change="Btnquery">最好点</el-radio>
                      <!-- <el-radio v-model="selobj.gtype" label="0" @change="getTableDataShow">所有点</el-radio> -->
                  </td>
                  <!-- <td align="right">水平</td> -->
                  <td></td>
                  <td align="left" colspan="5">
                      <el-checkbox-group v-model="selobj.qclevel" size="mini" style="text-align: left;">
                          <el-checkbox style="min-width:35px" v-for="(qclevel, index) in queryQclevels" :key="index" :label="qclevel.label">{{ qclevel.label }}</el-checkbox>
                      </el-checkbox-group>
                  </td>
                  <td></td>
              </tr>
              <tr>
                  <td colspan="12" align="right" style="text-align:right;width:100%">
                      <el-button icon="el-icon-search" v-has="'PINQUERY'" type="primary" @click="Btnquery">查询(F8)</el-button>
                      <el-button icon="el-icon-delete" @click="BtnClear">清 除(C)</el-button>
                      <el-button icon="el-icon-upload2" v-has="'PEXPORT'" @click="BtnExport()">导 出</el-button>
                      <el-button icon="el-icon-upload2" v-has="'PEXPORT'" @click="BtnExportTrans()">导出全部</el-button>
                      <el-button icon="el-icon-close" @click="BtnExc" type="warning">退出(E)</el-button>
                  </td>
              </tr>
          </table>
      </div>
      <div class="pagecenter">
          <div class="middle_center">
              <rm-layout-left splitcolor="#DoderBlue" movingcolor="#CCFFFF" :splitwidth="5" :fixsize="260">
                  <template v-slot:fix>
                      <div style="height:100%">
                          <vxe-table ref="LTable" width="100%" height="auto" :select-config="{ reserve: true }" :data.sync="qcitemData" border size="mini" :keyboard-config="keyConfig" :mouse-config="{ selected: true }" show-header-overflow highlight-current-row highlight-hover-row resizable stripe :edit-config="{ trigger: 'click', mode: 'cell', showStatus: true }" @current-change="rptitem_change">
                              <vxe-table-column field="itemcode_en" title="项目编号" width="40%" header-align="center"></vxe-table-column>
                              <vxe-table-column field="rpt_itemname" title="项目名称" width="60%" header-align="center"> </vxe-table-column>
                          </vxe-table>
                      </div>
                  </template>
                  <vxe-table :data="tableData" :loading="loading" :keyboard-config="keyConfig" :keyboard-local-config="keyLocalConfig" :select-config="selectConfig" :sort-config="{trigger: 'cell'}" :edit-config="{trigger: 'click', mode: 'cell'}" class="editable-footer" border fit height="auto" header-align="center" weight="auto" highlight-current-row highlight-hover-row highlight-hover-column ref="RTable" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe :key="xTablekey">
                      <vxe-table-column :key="item.field" :field="item.field" :title="item.title" v-for="item in tableColumn" align="center" :width="item.width" sortable :sort-width="item.field=='sampleno'?20:0"></vxe-table-column>
                  </vxe-table>
              </rm-layout-left>
          </div>
      </div>
  </div>
</template>


<script>
import XEUtils from "xe-utils"; //引入工具类
export default {
  name: "LIS616",
  data() {
      return {
          //*new
          tableColumn: {},
          queryQclevels: [], // ["水平1", "水平2", "水平3", "水平4", "水平5", "水平6"], //全部水平列表
          instrid: 0,
          xTablekey: 0,
          keyConfig: {
              isEdit: true,
          },
          keyLocalConfig: {
              isArrow: true,
              isDel: true,
              isTab: true,
          },
          selectConfig: { tigger: "row" },
          qcitemData: [],
          tableData: [],
          loading: false,
          //*初始化
          utype: "",
          //*初次查询
          rptIstLoading: false, //初次查询Loading
          rpt1stData: [], //初次报告数据
          // startdt: XEUtils.XEUtils.toDateString(new Date(), "yyyy-MM-01"),
          //初次查询条件
          selobj: {
              startdt: XEUtils.toDateString(new Date(), "yyyy-MM-01"),
              enddt: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
              instrid: 0,
              freq: "0",
              user: "0",
              rptitems: "0",
              point4: "0",
              gtype: "3", //3平均数,0所有点 、1最后点、 2最好点
              qclevel: [], //用户 勾选的水平
              queryqclevelcode: [], //用户勾选水平的编码
              // queryQclevelEnable: [], //允许用户勾选的水平
              tableColumn: [],
          },
          rptitemlist: "",
          rptitemnalist: "",
          instrname: "",
          exportTableColLen: 0,
      };
  },
  created() {
      this.BtnClear();
      for (let i = 1; i < 7; i++) {
          // for (let i = 1; i < 7; i++) {
          let val = {
              value: i,
              label: "水平 " + i,
          };
          if (i == 4) val.label = "低";
          if (i == 5) val.label = "中";
          if (i == 6) val.label = "高";
          this.queryQclevels.push(val);
      }
      this.queryqcitem();
  },
  mounted() {
      // this.initOption();
  },
  methods: {
      async query() {
          this.tableColumn = [];
          var row = this.$refs.LTable.getCurrentRow();
          if (row == null) return false;
          this.getQclevelCode(this.selobj.qclevel);
          try {
              const res = await this.$tool.request({
                  url: "LIS/LIS6/LIS616",
                  method: "post",
                  data: {
                      action: "query",
                      isex: 0,
                      ...this.selobj,
                      rptitemcode: row.rpt_itemcode,
                      queryqclevelcode: this.queryqclevelcode,
                  },
              });
              if (res != null) {
                  this.tableColumn = res.Cols;
                  this.tableData = res.Data;
              }
          } catch (e) {
              this.$message.warning(e);
          }
      },
      async BtnExportTrans() {
          this.getQclevelCode(this.selobj.qclevel);
          if (this.rptitemlist == "") return false;
          try {
              const res = await this.$tool.request({
                  url: "LIS/LIS6/LIS616",
                  method: "post",
                  data: {
                      action: "exporttrans",
                      isex: 0,
                      ...this.selobj,
                      rptitemlist: this.rptitemlist,
                      rptitemnalist: this.rptitemnalist,
                      queryqclevelcode: this.queryqclevelcode,
                  },
              });
              if (res != null) {
                  if (res.flag == "0") {
                      this.$alert(res.msg);
                  } else if (res.flag == "1") {
                      let p = {
                          prgno: "LIS616",
                          filename: res.folderna,
                          zipPath: res.zipPath,
                          filesPath: res.filesPath,
                      };
                      this.$pub.commexport(this, p, res.folderna + ".zip");
                      this.$message.success(res.msg);
                  }
              }
          } catch (e) {
                  this.$alert("导出失败！"+ e, "错误", {
                      confirmButtonText: "确定",
                  });
          }
      },
      //根据所选levelname获取level 编号 queryqclevelcode[]
      getQclevelCode(qclevel) {
          this.queryqclevelcode = [];
          for (var i = 0; i < qclevel.length; i++) {
              for (var m = 0; m < this.queryQclevels.length; m++) {
                  if (this.queryQclevels[m].label == qclevel[i]) {
                      this.queryqclevelcode.push(this.queryQclevels[m].value);
                      continue;
                  }
              }
          }
      },
      async queryqcitem() {
          this.qcitemData = [];
          this.tableData = [];
          this.queryQclevels = [];
          this.selobj.qclevel = [];
          this.rptitemlist = "";
          this.rptitemnalist = "";
          try {
              const res = await this.$tool.request({
                  url: "LIS/LIS6/LIS616",
                  method: "post",
                  data: {
                      action: "queryqcitem",
                      instrid: this.selobj.instrid,
                      queryDate1: this.selobj.startdt,
                      queryDate2: this.selobj.enddt,
                  },
              });
              if (res != null) {
                  if (res.qcitemdt != null) {
                      this.qcitemData = res.qcitemdt;
                      if (this.qcitemData.length > 0) {
                          this.$refs.LTable.setCurrentRow(this.qcitemData[0]);
                          this.qcitemData.forEach((e) => {
                              this.rptitemlist += "," + e.rpt_itemcode;
                              this.rptitemnalist += "," + e.rpt_itemname.replace(/[&\|\\\*^%$#@\/:*?"<>|/-]/g,"")+ "[" + e.itemcode_en + "]";
                          });

                          if (this.rptitemlist.length > 0) this.rptitemlist = this.rptitemlist.substring(1, this.rptitemlist.length);
                          if (this.rptitemnalist.length > 0) this.rptitemnalist = this.rptitemnalist.substring(1, this.rptitemnalist.length);
                          setTimeout((res) => {
                              this.query();
                          }, 50);
                      }
                  }
                  if (res.qclvdt != null) {
                      for (var i = 0; i < res.qclvdt.length; i++) {
                          // this.queryQclevelEnable[i] = data[i].val;
                          // this.queryQclevel[i] = data[i].val;
                          let val = {
                              value: res.qclvdt[i].val,
                              label: res.qclvdt[i].na,
                          };
                          this.queryQclevels.push(val);
                          // this.queryQclevelEnable.push(val);
                          this.selobj.qclevel.push(val.label);
                      }
                  }
              }
          } catch (e) {
              this.$message.warning(e);
          }
      },
      getTableDataShow() {},
      //左边项目切换
      rptitem_change({ row }) {
          this.query();
      },
      //? ****************初始化获取数据******************//
      //获取Option
      /*      rptunitOption           报告单元
              specimenOption      样本类型
              resultcombOption  多项目条件
      */
      async initOption() {
          try {
              const res = await this.$tool.request({
                  url: "LIS/LIS6/LIS616",
                  method: "post",
                  data: {
                      action: "getOption",
                  },
              });
              this.rptunitOption = res.rptunitOption;
              this.pattypeOption = res.pattypeOption;
              this.specimenOption = res.specimenOption;
              this.resultcombOption = res.resultcombOption;
              this.rescodeOption = res.rescodeOption;
              //读取缓存
              let jsonList = localStorage.getItem("rm_LIS616_ShowCols");
              if (jsonList != "" && jsonList != null) {
                  let tempOption = JSON.parse(jsonList);
                  this.selobj = tempOption;
              }
          } catch (e) {
              this.$message.warning(e);
          }
      },
      //TAB点击事件
      HandleClick(tab) {
          if (tab.label == "初次查询") {
              this.rpt1stkey++;
          } else if (tab.label == "二次查询") {
              this.rpt2ndkey++;
          }
      },
      //? ******************初次查询************************/
      //报告项目
      Rpt_itemChange(code, name) {
          this.selrow.rpt_itemcode = code;
          this.selrow.rpt_itemname = name;
          this.rpt_itemDlg = false;
      },
      //初次查询 用于显示第一次查询的病人基本信息及结果，及所有病人pat_no
      Btnquery() {
          this.query();
      },
      showColsChange() {
          let jsonList = JSON.stringify(this.selobj);
          localStorage.setItem("rm_LIS616_ShowCols", jsonList);
      },
      //清除
      BtnClear() {
          this.selobj = {
              startdt: XEUtils.toDateString(new Date(), "yyyy-MM-01"),
              enddt: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
              freq: "0",
              user: "0",
              rptitems: "0",
              point4: "0",
              gtype: "3", //-1平均数,0所有点 、1最后点、 2最好点
              qclevel: [], //用户 勾选的水平
          };
          this.tableData = [];
          this.tableColumn = [];
          this.queryqclevelcode = [];
      },
      instrChange(code, name) {
          this.selobj.instrid = code;
          this.instrname = name;
          this.queryqcitem();
      },
      BtnExport() {
          let data = [];
          data = this.getExportData();
          var exdata = [];
          if (data.length > 0) {
              for (var i = 0; i < data.length; i++) {
                  if (i != 0) exdata.push(data[i]);
              }
          }
          data = exdata;
          var row = this.$refs.LTable.getCurrentRow();
          if (row == null) return false;
          // let filtertxt = this.getExportFilter();
          var name = "[" + this.selobj.instrid + "]" + this.selobj.startdt + "-" + this.selobj.enddt + "-" + this.selobj.instrid + "-" + row.itemcode_en;
          this.$pub.exportTableExcelWithData(data, this.exportTableColLen, name + ".xls");
      },
      // 获取当前导出文件的所有查询条件
      getExportFilter() {
          let filtertxt = "质控日期:" + XEUtils.toDateString(this.selobj.startdt, "yyyy-MM-dd") + "至" + XEUtils.toDateString(this.selobj.enddt, "yyyy-MM-dd") + "; ";
          if(this.selobj.instrid) filtertxt += "检验仪器:" + this.instrname + "; ";
          if(this.selobj.freq == "1") filtertxt += "显示次数; ";
          if(this.selobj.user == "1") filtertxt += "显示上报人; ";
          if(this.selobj.point4 == "1") filtertxt += "精确度小数点后4位; ";
          if(this.selobj.gtype) filtertxt += "数据模式:" + (this.selobj.gtype == "1" ? "最后点" : this.selobj.gtype == "2" ? "最好点" : "平均值") + "; ";
          if(this.selobj.qclevel.length > 0)
          {
              this.selobj.qclevel.forEach((item) => {
                  filtertxt += item + "; ";
              });
          }
          return filtertxt;
      },
      getExportData() {
          let datas = [];
          let tableColumn = [];
          datas = this.$refs.RTable.afterFullData;
          var allcolumns = this.$refs.RTable.getColumns();
          for (let i = 0; i < allcolumns.length; i++) {
              tableColumn.push({
                  field: allcolumns[i].property,
                  title: allcolumns[i].title,
              });
          }
          this.exportTableColLen = tableColumn.length;
          let columns = tableColumn.filter((item) => item.field);
          let headers = [columns.map((item) => item.title)];
          return headers.concat(
              datas.map((row) => {
                  return columns.map((column) => {
                      return row[column.field];
                  });
              })
          );
      },
      BtnExc() {
          let index = this.$parent.$refs.itemtags.tagsList.findIndex((m) => m.name == "LIS616");
          this.$parent.$refs.itemtags.tagsList.splice(index, 1);
          this.$router.go(-1);
      },
      popMultipleChk_R(e, fieldname, option, title, type) {
          this.seartype = type;
          let temp = XEUtils.clone(option, true);
          this.option1 = [];
          this.option2 = [];
          if (!e) {
              let k = e.split(",");
              k.forEach((item) => {
                  let ndata = temp.find((m) => m.data == item);
                  if (ndata != undefined && ndata != null) {
                      this.option2.push(ndata);
                  }
              });
              temp.forEach((item) => {
                  if (k.indexOf(item.data + "") < 0) {
                      this.option1.push(item);
                  }
              });
          } else {
              this.option1 = temp;
          }
          this.fieldname = fieldname;
          this.dialogMultipleVisible_R = true;
      },
      closedialog_R(value, name) {
          var tablename = "com1stTable";
          if (this.seartype == "1") {
              tablename = "com1stTable";
          } else if (this.seartype == "2") {
              tablename = "com2ndTable";
          }
          if (value != "close") {
              let row = this.$refs[tablename].getCurrentRow();
              if (row) {
                  row.resistrules = value;
              }
          }
          this.dialogMultipleVisible_R = false;
      },
  },
  //快捷键 暂时不提示使用吧
  beforeRouteEnter(to, from, next) {
      next((vm) => {
          let that = vm;
          let altArr = [];
          document.onkeydown = function (e) {
              let key = window.event.keyCode;
              //*组合按键
              if (altArr.length > 0) {
                  //65到90 a到z（A到Z）
                  // //*Alt + C 清除
                  if (key == 67) {
                      that.BtnClear();
                  }
                  //*Alt + E 退出
                  if (key == 69) {
                      that.BtnExc();
                  }
              } else {
                  if (key == 119) {
                      //F8
                      that.Btnquery();
                  } else if (key == 120) {
                      //F9
                      that.Btn2ndSel(e);
                  } else if (key == 121) {
                      //F10
                      if (that.b2ndDlg) {
                          that.Btn2ndQueryCol();
                      }
                  } else if (key == 18) {
                      //按下了alt
                      altArr.push(key);
                  }
              }
          };
          document.onkeyup = function (e) {
              let key = window.event.keyCode;
              if (key == 18) {
                  altArr = [];
              }
          };
      });
  },
};
</script>

<style scoped>
::v-deep .el-checkbox__inner::after {
  border: 2px solid #000000 !important;
  box-sizing: content-box;
  content: "";
  border-left: 0 !important;
  border-top: 0 !important;
  height: 7px;
  left: 4px;
  position: absolute;
  top: 1px;
  transform: rotate(45deg) scaleY(0);
  width: 3px;
  transition: transform 0.15s ease-in 0.05s;
  transform-origin: center;
}
::v-deep .vxe-table--header .vxe-header--column {
  border-right: 1px solid #cccccc;
  border-bottom: 1px solid #ccc;
}
::v-deep .el-checkbox__input.is-disabled + span.el-checkbox__label {
  color: #000000;
  cursor: not-allowed;
}
::v-deep .el-checkbox__input.is-disabled .el-checkbox__inner,
.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner + .el-input.is-disabled .el-input__inner {
  background-color: #ffffff !important;
}
.pagetop {
  left: 0;
  right: 0;
  display: -webkit-box;
  -webkit-box-pack: center;
  -webkit-box-align: center;
  border: solid 1px #ccc;
  position: absolute;
  z-index: 99;
  overflow-x: auto;
  white-space: nowrap;
}
.pagecenter {
  position: absolute;
  top: 106px;
  bottom: 0px;
  overflow: auto;
  width: 100%;
  left: 0px;
}
.middle_center {
  position: absolute;
  height: 100%;
  right: 0px;
  left: 0px;
  overflow: auto;
}
.middle-right {
  position: absolute;
  top: 0;
  bottom: 0px;
  overflow: visible;
  width: 570px;
  right: 0px;
}
table tr td:nth-child(odd) {
  text-align: right;
  white-space: nowrap;
}
table tr td:nth-child(even) {
  text-align: left;
}
table tr td:nth-child(1) {
  width: 80px;
}
table {
  font-size: 12px;
  border-collapse: separate;
  border-spacing: 5px;
}
.middle-center {
  height: 250px;
}
::v-deep .el-input--suffix .el-input__inner {
  padding-right: 0;
}
::v-deep .el-textarea__inner {
  padding: 0px 5px;
}
</style>
